﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace AutomaticPush.Models
{
    public class ApiResult<T>
    {
        public T Data { get; set; }
        public HttpStatusCode? StatusCode { get; set; }
        public ErrorObj Errors { get; set; }
        public string ErrorMsg { get; set; }

        public ApiResult()
        {
            Data = default(T);
        }

        /// <summary>
        /// 接口是否正常响应
        /// </summary>
        public bool IsSuccess
        {
            get
            {
                if (this != null && this.StatusCode == HttpStatusCode.OK)
                    return true;
                return false;
            }
        }
    }


    public class ErrorObj
    {
        public Dictionary<string, string> Form { get; set; }
        public List<string> Message { get; set; }

        public string GetFirstError()
        {
            if (Message != null && Message.Any())
            {
                return Message.First();
            }
            if (Form != null && Form.Any())
            {
                return Form.First().Value;
            }
            return "";
        }
    }
}
